package com.mashibing.apipassenger.service.impl;

import com.mashibing.apipassenger.service.ServiceSmsRestTemplateService;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.dto.servicesms.SmsTemplateDto;
import com.mashibing.internalcommon.dto.servicesms.request.SmsSendRequest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author gorkr
 * @date 2022/05/29 09:46
 **/
@Service
public class ServiceSmsRestTemplateServiceImpl implements ServiceSmsRestTemplateService {
    @Resource
    private RestTemplate restTemplate;

    @Override
    public ResponseResult sendSms(String phoneNumber, String code) {

        String http = "http://";
        String serviceName = "SERVICE-SMS";
        String uri = "/send/sms-template";
        String url = http + serviceName + uri;

        SmsSendRequest smsSendRequest = new SmsSendRequest();
        // 创建一个string类型的数组对象比如： String a[] = new String[3]{"aa","bb","cc"}
        String[] phoneNumbers = new String[] {phoneNumber};
        smsSendRequest.setReceivers(phoneNumbers);

        List<SmsTemplateDto> data = new ArrayList<>();
        SmsTemplateDto dto = new SmsTemplateDto();
        dto.setId("SMS_144145499");
        int templateSize = 1;
        HashMap<String,Object> templateMap = new HashMap<>(templateSize);
        templateMap.put("code", code);
        dto.setTemplateMap(templateMap);
        data.add(dto);

        smsSendRequest.setData(data);


        return restTemplate.postForObject(url,smsSendRequest,ResponseResult.class);
    }

    @Override
    public ResponseResult generatorCode(int identity, String phoneNumber) {

        String url = "http://service-verification-code/verify-code/generate/" + identity + "/" + phoneNumber;
        return restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<Object>(null, null), ResponseResult.class).getBody();


    }
}
    